<template>
  <div class="login">
    <!-- <van-button type="primary">按钮</van-button>
    <van-checkbox :checked="true">复选框</van-checkbox> -->
    <cp-nav-bar
      title="登录"
      right-text="祝1"
      @click-right="clickRight"
    ></cp-nav-bar>
    <div class="login">
      <div class="login-head">
        <h3>密码登录</h3>
        <a href="javascript:;">
          <span>短信验证码登录</span>
          <van-icon name="arrow"></van-icon>
        </a>
      </div>
      <!-- == form 表单 == -->
      <van-form autocomplete="off" @submit="login">
        <van-field
          :rules="mobileRules"
          v-model="mobile"
          placeholder="请输入手机号"
          type="tel"
        ></van-field>
        <van-field
          :rules="passwordRules"
          v-model="password"
          placeholder="请输入密码"
          type="password"
        >
        </van-field>
        <div class="cp-cell">
          <van-checkbox v-model="agree">
            <span>我已同意</span>
            <a href="javascript:;">用户协议</a>
            <span>及</span>
            <a href="javascript:;">隐私条款</a>
          </van-checkbox>
        </div>
        <div class="cp-cell">
          <van-button block round type="primary" native-type="submit"
            >登 录</van-button
          >
        </div>
        <div class="cp-cell">
          <a href="javascript:;">忘记密码？</a>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { mobileRules, passwordRules } from "@/utils/rules.ts";

import { showFailToast, showSuccessToast } from "vant";
import { loginByPassword } from "@/api/user";
import { useUserStore } from "@/stores/modules/user";
import { useRoute, useRouter } from "vue-router";

const agree = ref(false);
const mobile = ref("13230000008");
const password = ref("abc12345");
const store = useUserStore();
const router = useRouter();
const route = useRoute();
//点击登录
const login = async () => {
  if (!agree.value) return showFailToast("请勾选我已同意");
  // if (!agree.value) return Toast("请勾选我已同意");
  console.log("校验通过");
  try {
    const res = await loginByPassword(mobile.value, password.value);
    // console.log("登录", res);
    // console.log("登录22", res.token);
    store.setUser(res);
    router.push((route.query.returnUrl as string) || "/user"); //断言确认类型
    showSuccessToast("登录成功");
  } catch (error) {
    console.log(error);
  }
};
const clickRight = () => {
  console.log("点击了右边的按钮");
};
//控制密码显隐
// const isShowPass = ref(false);
</script>

<style lang="scss" scoped>
.login {
  padding-top: 46px;
  &-head {
    padding: 30px 30px 50px;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  .van-form {
    padding: 0 14px;
    .cp-cell {
      height: 52px;
      padding: 14px 16px;
      box-sizing: border-box;
      display: flex;
      align-items: center;
      .van-checkbox {
        a {
          color: var(--cp-primary);
          padding: 0 5px;
        }
      }
    }
  }
}
</style>